package com.timestar.floating;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.timestar.floating.listener.ITouchButton;
import com.timestar.floating.utils.ScreenUtils;
import com.timestar.floating.utils.TouchButtonHelper;

/**
 * Created by zxn on 2025/2/8.
 */
public class FloatingButton
        extends AppCompatImageView implements View.OnTouchListener, ITouchButton {

    /**
     * 触摸滑动后手指相对屏幕左上角的位置坐标
     */
    private float rawX;
    private float rawY;

    /**
     * 触摸点按下相对view的左上角的位置
     * 用于跟滑动后手指相对屏幕左上角位置计算view相对屏幕的位置x、y
     */
    private int downX;
    private int downY;
    private RectF mBorderRectF;
    private OnClickListener mFloatingClickListener;

    public FloatingButton(Context context) {
        super(context);
        onInit();
    }

    private void onInit() {
        //setOnTouchListener(this);
        TouchButtonHelper touchLayoutHelper = new TouchButtonHelper(this);
        touchLayoutHelper.registerObservedView(this);
        int width = ScreenUtils.getScreenWidth(getContext());
        int height = ScreenUtils.getScreenHeight(getContext());
        int top = ScreenUtils.getStatusHeight(getContext()) /*+ getHeight() * 2*/;
        mBorderRectF = new RectF(0, top, width, height);
        //对外暴漏点点击事件、
        touchLayoutHelper.setOnSwipeListener(view -> {
            if (mFloatingClickListener != null) {
                mFloatingClickListener.onClick(view);
            }
        });
    }

    public FloatingButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        onInit();
    }

    public FloatingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        onInit();
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                downY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                actionMove(event);
                break;
            case MotionEvent.ACTION_UP:
                //actionUp(event);
                break;
            case MotionEvent.ACTION_OUTSIDE://手指点击窗口外响应该事件:
                //actionOutSide(event);
                break;
        }
        return false;
    }

    private void actionMove(MotionEvent event) {
        //拖动事件下一直计算坐标 然后更新悬浮窗位置
        //获取触摸点相对于屏幕左上角的坐标
        rawX = event.getRawX();
        rawY = event.getRawY() - getStatusBarHeight();
        float newX = rawX - downX;
        float newY = rawY - downY;

        animateView(newX, newY);

    }

    /**
     * 用于获取系统状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    private int getStatusBarHeight() {
        int identifier = Resources.getSystem().getIdentifier("status_bar_height",
                "dimen", "android");
        if (identifier > 0) {
            return Resources.getSystem().getDimensionPixelSize(identifier);
        }
        return 0;
    }

    private void animateView(float x, float y) {
        animate().setInterpolator(new AccelerateInterpolator())
                .x(x)
                .y(y)
                .alpha(1f)
                .setDuration(0)
                .start();
    }

    @Override
    public boolean isLayoutEnabled() {
        return true;
    }

    @Override
    public RectF borderRectF() {
        return mBorderRectF;
    }

    public void setFloatingClickListener(View.OnClickListener listener) {
        mFloatingClickListener = listener;
    }
}
